/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bot_jae;

/**
 *
 * @author Eric Momsen
 */
public class Board {
    
    public String[][] board_state = new String[8][8];
    public int column;
    public int row;
    public int c_offset;
    public int r_offset;
    public char dir;
    public int ply_num = 0; //0 for gold to move, 1 for silver to move, 2 for all later moves

    void makeMove(String move_text) {
        System.out.println("got to make move");
        String[] steps = move_text.split(" ");
        
        
        if(steps.length < 8) { //move pieces on board
            //move_text will be like:  hb7s hg7s ed7s dd8s rf3x
            for (int i = 0; i < steps.length; i++) {
                column = Character.getNumericValue(steps[i].charAt(1))-10;
                row = steps[i].charAt(2)-49;
                board_state[column][row] = "0"; //remove piece
                place(steps[i]); //put it back on board in new place
            }
        }
        else{  //first move of game, just add pieces to board
            //move_text: Rh1 Rg1 Rf1 Rc1 Rb1 Ra1 Rh2 Ra2 Ce1 Cf2 Dd1 Dc2 Hg2 Hb2 Md2 Ee2
            //or  ed7 hg7 hb7 me7 de8 dd8 cf7 cc7 ra7 rh7 ra8 rb8 rc8 rf8 rg8 rh8
            for (int i = 0; i < steps.length; i++) {
                column = Character.getNumericValue(steps[i].charAt(1))-10;
                row = steps[i].charAt(2)-49;
                place(steps[i]);
            }
        }
        ply_num++;
        System.out.println("printed ply num: " + ply_num);
    }

    private void place(String step) {
        
        c_offset = 0;
        r_offset = 0;
        
        if (step.length()==4){
            dir = step.charAt(3);
            switch (dir) {
                case 'n':
                    r_offset = 1;
                    break;
                case 's':
                    r_offset = -1;
                    break;
                case 'e':
                    c_offset = 1;
                    break;
                case 'w':
                    c_offset = -1;
                    break;
                case 'x':
                    //nothing - this is for a capture
                    break;
            } 
        }  //end if for direction offset
        
        board_state[row+r_offset][column+c_offset] = get_name(step.charAt(0));
    }

    private String get_name(char letter) {
        //there must be a better way to do this?
        //saving the prolog names into the board state array
        //so need to convert R to rabbit(goodGuy), etc.
        switch (letter){
            case 'E':
                return "elephant(goodGuy)";
            case 'M':
                return "camel(goodGuy)";
            case 'H':
                return "horse(goodGuy)";
            case 'D':
                return "dog(goodGuy)";
            case 'C':
                return "cat(goodGuy)";
            case 'R':
                return "rabbit(goodGuy)";
            case 'e':
                return "elephant(badGuy)";
            case 'm':
                return "camel(badGuy)";
            case 'h':
                return "horse(badGuy)";
            case 'd':
                return "dog(badGuy)";
            case 'c':
                return "cat(badGuy)";
            case 'r':
                return "rabbit(badGuy)";                
        }
        return ""; //shouldn't get here!
    }

    void clear() {
        ply_num = 0;
        
        for (int i = 0; i < board_state.length; i++) {
            for (int j=0; j < board_state[i].length; j++) {
            board_state[i][j] = "0";    
            }
        }
    }

    String plString() {
        String plString = "state([";
        for (int i = 0; i < board_state.length; i++) {
            plString = plString + "[";
            for (int j = 0; j < board_state[i].length; j++){
                plString = plString + board_state[i][j];
                if(j!=7) plString = plString + ",";
            }
            plString = plString + "]";
            if(i!=7) plString = plString + ",";
        }
        plString = plString + "],";
        
        //right now prolog expects a list of pieces
        
        String guy = "goodGuy";
        boolean first = true;
        String piece = "";
        for (int g = 0; g<=1; g++){
            plString = plString + "[";
            for (int i = 0; i < board_state.length; i++) {
                for (int j = 0; j < board_state[i].length; j++){
                    if(board_state[i][j].contains(guy)){
                        if(!first) plString = plString + ","; else first=false;
                        piece = board_state[i][j].substring(0, board_state[i][j].indexOf('('))
                                +"("+j+","+i+")"
                                ;
                        plString = plString + piece;
                    }
                }
            }
            plString = plString + "]";
            if(g==0) plString = plString + ",";
            guy="badGuy";
            first = true;
        }
        plString = plString + ")";
        return plString;
    }
}